home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / read_pwd.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  9KB  |  381 lines

  1. /* lib/des/read_pwd.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. /* 06-Apr-92 Luke Brennan    Support for VMS */
  49. #include "des_locl.h"
  50. #include <signal.h>
  51. #include <string.h>
  52. #include <setjmp.h>
  53.  
  54. /* There are 5 types of terminal interface supported,
  55.  * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
  56.  */
  57.  
  58. #if defined(sgi) || defined(__sgi)
  59. #define TERMIOS
  60. #undef TERMIO
  61. #undef SGTTY
  62. #endif
  63.  
  64. #ifdef _LIBC
  65. #define TERMIO
  66. #endif
  67.  
  68. #if !defined(TERMIO) && !defined(TERMIOS) && !defined(VMS) && !defined(MSDOS)
  69. #define SGTTY
  70. #endif
  71.  
  72. #ifdef TERMIOS
  73. #include <termios.h>
  74. #define TTY_STRUCT        struct termios
  75. #define TTY_FLAGS        c_lflag
  76. #define    TTY_get(tty,data)    tcgetattr(tty,data)
  77. #define TTY_set(tty,data)    tcsetattr(tty,TCSANOW,data)
  78. #endif
  79.  
  80. #ifdef TERMIO
  81. #include <termio.h>
  82. #define TTY_STRUCT        struct termio
  83. #define TTY_FLAGS        c_lflag
  84. #define TTY_get(tty,data)    ioctl(tty,TCGETA,data)
  85. #define TTY_set(tty,data)    ioctl(tty,TCSETA,data)
  86. #endif
  87.  
  88. #ifdef SGTTY
  89. #include <sgtty.h>
  90. #define TTY_STRUCT        struct sgttyb
  91. #define TTY_FLAGS        sg_flags
  92. #define TTY_get(tty,data)    ioctl(tty,TIOCGETP,data)
  93. #define TTY_set(tty,data)    ioctl(tty,TIOCSETP,data)
  94. #endif
  95.  
  96. #ifndef _LIBC
  97. #include <sys/ioctl.h>
  98. #endif
  99.  
  100. #ifdef MSDOS
  101. #include <conio.h>
  102. #define fgets(a,b,c) noecho_fgets(a,b,c)
  103. #endif
  104.  
  105. #ifdef VMS
  106. #include <ssdef.h>
  107. #include <iodef.h>
  108. #include <ttdef.h>
  109. #include <descrip.h>
  110. struct IOSB {
  111.     short iosb$w_value;
  112.     short iosb$w_count;
  113.     long  iosb$l_info;
  114.     };
  115. #endif
  116.  
  117. #ifndef NX509_SIG
  118. #define NX509_SIG 32
  119. #endif
  120.  
  121. #ifdef PROTO
  122. static void read_till_nl(FILE *);
  123. static int read_pw(char *buf, char *buff, int size, char *prompt, int verify);
  124. static void recsig(int);
  125. static void pushsig(void);
  126. static void popsig(void);
  127. #ifdef MSDOS
  128. static int noecho_fgets(char *buf, int size, FILE *tty);
  129. #endif
  130. #else
  131. static void read_till_nl();
  132. static int read_pw();
  133. static void recsig();
  134. static void pushsig();
  135. static void popsig();
  136. #ifdef MSDOS
  137. static int noecho_fgets();
  138. #endif
  139. #endif
  140.  
  141. static void (*savsig[NX509_SIG])();
  142. static jmp_buf save;
  143.  
  144. int des_read_password(key, prompt, verify)
  145. des_cblock (*key);
  146. char *prompt;
  147. int verify;
  148.     {
  149.     int ok;
  150.     char buf[BUFSIZ],buff[BUFSIZ];
  151.  
  152.     if ((ok=read_pw(buf,buff,BUFSIZ,prompt,verify)) == 0)
  153.         des_string_to_key(buf,key);
  154.     memset(buf,0,BUFSIZ);
  155.     memset(buff,0,BUFSIZ);
  156.     return(ok);
  157.     }
  158.  
  159. int des_read_2passwords(key1, key2, prompt, verify)
  160. des_cblock (*key1);
  161. des_cblock (*key2);
  162. char *prompt;
  163. int verify;
  164.     {
  165.     int ok;
  166.     char buf[BUFSIZ],buff[BUFSIZ];
  167.  
  168.     if ((ok=read_pw(buf,buff,BUFSIZ,prompt,verify)) == 0)
  169.         des_string_to_2keys(buf,key1,key2);
  170.     memset(buf,0,BUFSIZ);
  171.     memset(buff,0,BUFSIZ);
  172.     return(ok);
  173.     }
  174.  
  175. int des_read_pw_string(buf, length, prompt, verify)
  176. char *buf;
  177. int length;
  178. char *prompt;
  179. int verify;
  180.     {
  181.     char buff[BUFSIZ];
  182.     int ret;
  183.  
  184.     ret=read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
  185.     memset(buff,0,BUFSIZ);
  186.     return(ret);
  187.     }
  188.  
  189. static void read_till_nl(in)
  190. FILE *in;
  191.     {
  192. #define SIZE 4
  193.     char buf[SIZE+1];
  194.  
  195.     do    {
  196.         fgets(buf,SIZE,in);
  197.         } while (strchr(buf,'\n') == NULL);
  198.     }
  199.  
  200. /* return 0 if ok, 1 (or -1) otherwise */
  201. static int read_pw(buf, buff, size, prompt, verify)
  202. char *buf;
  203. char *buff;
  204. int size;
  205. char *prompt;
  206. int verify;
  207.     {
  208. #ifdef VMS
  209.     struct IOSB iosb;
  210.     $DESCRIPTOR(terminal,"TT");
  211.     long tty_orig[3], tty_new[3];
  212.     long status;
  213.     unsigned short channel = 0;
  214. #else
  215. #ifndef MSDOS
  216.     TTY_STRUCT tty_orig,tty_new;
  217. #endif
  218. #endif
  219.     int number=5;
  220.     int ok=0;
  221.     int ps=0;
  222.     FILE *tty=NULL;
  223.     char *p;
  224.  
  225. #ifndef MSDOS
  226.     if ((tty=fopen("/dev/tty","r")) == NULL)
  227.         tty=stdin;
  228. #else /* MSDOS */
  229.     if ((tty=fopen("con","r")) == NULL)
  230.         tty=stdin;
  231. #endif /* MSDOS */
  232.  
  233. #if defined(TTY_get) && !defined(VMS)
  234.     if (TTY_get(fileno(tty),&tty_orig) == -1)
  235.         return(-1);
  236.     memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
  237. #endif
  238. #ifdef VMS
  239.     status = SYS$ASSIGN(&terminal,&channel,0,0);
  240.     if (status != SS$_NORMAL)
  241.         return(-1);
  242.     status=SYS$QIOW(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
  243.     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  244.         return(-1);
  245. #endif
  246.  
  247.     if (setjmp(save))
  248.         {
  249.         ok=0;
  250.         goto error;
  251.         }
  252.     pushsig();
  253.     ps=1;
  254.  
  255. #ifdef TTY_FLAGS
  256.     tty_new.TTY_FLAGS &= ~ECHO;
  257. #endif
  258.  
  259. #if defined(TTY_set) && !defined(VMS)
  260.     if (TTY_set(fileno(tty),&tty_new) == -1)
  261.         return(-1);
  262. #endif
  263. #ifdef VMS
  264.     tty_new[0] = tty_orig[0];
  265.     tty_new[1] = tty_orig[1] | TT$M_NOECHO;
  266.     tty_new[2] = tty_orig[2];
  267.     status = SYS$QIOW(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
  268.     if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  269.         return(-1);
  270. #endif
  271.     ps=2;
  272.  
  273.     while ((!ok) && (number--))
  274.         {
  275.         fputs(prompt,stderr);
  276.         fflush(stderr);
  277.  
  278.         buf[0]='\0';
  279.         fgets(buf,size,tty);
  280.         if (feof(tty)) goto error;
  281.         if ((p=(char *)strchr(buf,'\n')) != NULL)
  282.             *p='\0';
  283.         else    read_till_nl(tty);
  284.         if (verify)
  285.             {
  286.             fprintf(stderr,"\nVerifying password %s",prompt);
  287.             fflush(stderr);
  288.             buff[0]='\0';
  289.             fgets(buff,size,tty);
  290.             if (feof(tty)) goto error;
  291.             if ((p=(char *)strchr(buff,'\n')) != NULL)
  292.                 *p='\0';
  293.             else    read_till_nl(tty);
  294.                 
  295.             if (strcmp(buf,buff) != 0)
  296.                 {
  297.                 fprintf(stderr,"\nVerify failure");
  298.                 fflush(stderr);
  299.                 break;
  300.                 /* continue; */
  301.                 }
  302.             }
  303.         ok=1;
  304.         }
  305.  
  306. error:
  307.     fprintf(stderr,"\n");
  308.     /* What can we do if there is an error? */
  309. #if defined(TTY_set) && !defined(VMS) 
  310.     if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
  311. #endif
  312. #ifdef VMS
  313.     if (ps >= 2)
  314.         status = SYS$QIOW(0,channel,IO$_SETMODE,&iosb,0,0
  315.             ,tty_orig,12,0,0,0,0);
  316. #endif
  317.     
  318.     if (ps >= 1) popsig();
  319.     if (stdin != tty) fclose(tty);
  320. #ifdef VMS
  321.     status = SYS$DASSGN(channel);
  322. #endif
  323.     return(!ok);
  324.     }
  325.  
  326. static void pushsig()
  327.     {
  328.     int i;
  329.  
  330.     for (i=1; i<NX509_SIG; i++)
  331.         savsig[i]=signal(i,recsig);
  332.     }
  333.  
  334. static void popsig()
  335.     {
  336.     int i;
  337.  
  338.     for (i=1; i<NX509_SIG; i++)
  339.         signal(i,savsig[i]);
  340.     }
  341.  
  342. static void recsig(i)
  343. int i;
  344.     {
  345.     longjmp(save,1);
  346. #ifdef LINT
  347.     i=i;
  348. #endif
  349.     }
  350.  
  351. #ifdef MSDOS
  352. static int noecho_fgets(buf,size,tty)
  353. char *buf;
  354. int size;
  355. FILE *tty;
  356.     {
  357.     int i,n;
  358.     char *p;
  359.  
  360.     p=buf;
  361.     for (;;)
  362.         {
  363.         if (size == 0)
  364.             {
  365.             *p='\0';
  366.             break;
  367.             }
  368.         size--;
  369.         i=getch();
  370.         if (i == '\r') i='\n';
  371.         *(p++)=i;
  372.         if (i == '\n')
  373.             {
  374.             *p='\0';
  375.             break;
  376.             }
  377.         }
  378.     return(strlen(buf));
  379.     }
  380. #endif
  381.